﻿

LoadJS("/!js/uploadify-v2.1.4/swfobject.js");
LoadJS("/!js/uploadify-v2.1.4/jquery.uploadify.v2.1.4.js");
LoadJS("/!js/cutpic/cutpic.js");
LoadCSS("/!js/uploadify-v2.1.4/uploadify.css");

$(function () {
    var tempFolder = $("[id$=tempUrl]").val();       //上传临时目录   
    $('#uploadify').uploadify({
        'uploader': '/!js/uploadify-v2.1.4/uploadify.swf',
        'script': '/Handler/UploadAvatarHandler.ashx',
        'cancelImg': '/!js/uploadify-v2.1.4/cancel.png',
        'folder': tempFolder,
        // 'queueID': '',
        'auto': true,
        'multi': false,
        'method': 'get',
        'fileExt': '*.jpg;*.png',
        'buttonImg': '/!js/uploadify-v2.1.4/btn_uploadimg.jpg',
        'fileDesc': '请选择jpg , png文件...',
        'scriptData': { 'ASPSESSID': $('[id$=hdSessionId]').val() },
        'sizeLimit': 2097152,
        'onComplete': function (event, queueID, fileObj, response, data) {

            if (response.indexOf('Temp') != -1) {
                $("#bgDiv img").remove();                      //移除截图区里image标签                       
                $("#imgBtnSave").show();                          //保存按钮显示                                    
                var result = response.split('$');              //得返回参数               
                var maxVal = 0;
                if (parseInt(result[1]) > parseInt(result[2])) {
                    maxVal = result[2];
                }
                else {
                    maxVal = result[1];
                }
                $("[id$=maxVal]").val(maxVal);                     //设置截图区大小              
                $("[id$=hidImageUrl]").val(result[0]);             //上传路径存入隐藏域

                ShowImg(result[0] + "?" + Math.random(), result[1], result[2]);       //在截图区显示
                $("#uploadify").uploadifySettings('scriptData', { 'ASPSESSID': $('[id$=hdSessionId]').val(), 'LastImgUrl': $('[id$=hidImageUrl]').val() }); 	  //更新参数                        
            }
            else {
                alert(response);
            }
        }
    });

    $("#imgBtnSave").click(function () {
        $.ajax({
            type: "POST",
            url: "/Handler/CutAvatarHandler.ashx",
            data: { imgUrl: $('#hidImageUrl').val(), pointX: $("[id$=pointX]").val(), pointY: $("[id$=pointY]").val(), maxVal: $("[id$=maxVal]").val() },
            success: function (msg) {
                if (msg.indexOf('User') != -1) {
                    $("[id$=imgCut]").attr("src", msg + "?" + Math.random());
                    $('#result').show().text('头像上传成功！').addClass('correctinfo'); //上传成功显示提示信息   Add By Vinlon
                }
                else {
                    $('#result').show().text('头像上传失败！').addClass('errorinfo');
                }

            },
            error: function (xhr, msg, e) {
                alert("error");
            }
        });
    });

    function ShowImg(imagePath, imgWidth, imgHeight) {
        var imgPath = imagePath != "" ? imagePath : "!images/ef_pic.jpg";
        var ic = new ImgCropper("bgDiv", "dragDiv", imgPath, imgWidth, imgHeight, null);
    }
});